package cz.proks.schoolpupilbook.gwt.widget.common;

import java.util.LinkedList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

import cz.proks.schoolpupilbook.gwt.core.SchoolPupilBookFactory;
import cz.proks.schoolpupilbook.shared.domain.User;

public class UserRelationForm extends Composite {
	
	interface UserRelationFormUiBinder extends UiBinder<Widget, UserRelationForm> {
	}

	private static UserRelationFormUiBinder uiBinder = GWT.create(UserRelationFormUiBinder.class);	

	@UiField Errors errors;
	@UiField Grid table;
	@UiField TextBox idBox;
	@UiField ListBox parentListBox;
	@UiField ListBox studentListBox;
	
	public UserRelationForm() {
		initWidget(uiBinder.createAndBindUi(this));
	}
	
	public String getId() {
		return idBox.getValue();
	}
	
	public void setId(String id) {
		this.idBox.setValue(id);
	}
	
	public String getParentId() {
		int i = parentListBox.getSelectedIndex();
		return i < 0 ? null : parentListBox.getValue(i);
	}
	
	public void setParentId(String id) {
		int itemCount = parentListBox.getItemCount();
		for (int i = 0; i < itemCount; i++) {
			if (parentListBox.getValue(i).equals(id)) {
				parentListBox.setSelectedIndex(i);
				break;
			}
		}
	}
	
	public void setParents(LinkedList<User> parentList) {
		parentListBox.clear();
		
		if (parentList != null)
			for (User user : parentList) {
				parentListBox.addItem(user.getFullName(), user.getId());
			}
	}
	
	public String getStudentId() {
		int i = studentListBox.getSelectedIndex();
		return i < 0 ? null : studentListBox.getValue(i);
	}
	
	public void setStudentId(String id) {
		int itemCount = studentListBox.getItemCount();
		for (int i = 0; i < itemCount; i++) {
			if (studentListBox.getValue(i).equals(id)) {
				studentListBox.setSelectedIndex(i);
				break;
			}
		}
	}
	
	public void setStudents(LinkedList<User> studentList) {
		studentListBox.clear();
		
		if (studentList != null)
			for (User user : studentList) {
				studentListBox.addItem(user.getFullName(), user.getId());
			}
	}
	
	public void setIdReadOnly(boolean value) {
		idBox.setReadOnly(value);
	}
	
	public void setParentReadOnly(boolean value) {
		parentListBox.setEnabled(value);
	}
	
	public void setStudentReadOnly(boolean value) {
		studentListBox.setEnabled(value);
	}
	
	@UiHandler("submitButton")
	public void submit(ClickEvent event) {
		LinkedList<String> errors = validate();
		if (errors.isEmpty()) {
			this.errors.setErrors(null);
			SubmitEvent submitEevent = new SubmitEvent(getId(), getParentId(), getStudentId());
			SchoolPupilBookFactory.getEventBus().fireEvent(submitEevent);			
		} else {
			this.errors.setErrors(errors);			
		}
	}
	
	@UiHandler("backButton")
	public void back(ClickEvent event) {
		History.back();
	}
	
	private LinkedList<String> validate() {
		LinkedList<String> errors = new LinkedList<String>();
		
		if (getParentId() == null || getParentId().isEmpty()) {
			errors.add("No parent selected");
		}
		
		if (getStudentId() == null || getStudentId().isEmpty()) {
			errors.add("No student selected");
		}
		
		return errors;
	}
	
	public static class SubmitEvent extends GwtEvent<SubmitEvent.SubmitEventHandler> {
		public interface SubmitEventHandler extends EventHandler {
			void onSubmit(SubmitEvent event);
		}
		
		public final static Type<SubmitEvent.SubmitEventHandler> TYPE = new Type<SubmitEvent.SubmitEventHandler>();
		
		private String id;
		private String parentId;
		private String studentId;
		
		public SubmitEvent(String id, String parentId, String studentId) {
			super();
			this.id = id;
			this.parentId = parentId;
			this.studentId = studentId;
		}
		
		@Override
		public Type<SubmitEventHandler> getAssociatedType() {
			return TYPE;
		}

		@Override
		protected void dispatch(SubmitEventHandler handler) {
			handler.onSubmit(this);			
		}

		public String getId() {
			return id;
		}

		public String getParentId() {
			return parentId;
		}

		public String getStudentId() {
			return studentId;
		}		
	}
}
